package com.nic.view.widget;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.nic.view.R;
import com.nic.view.adapter.CommonCheckAdapter;
import com.nic.view.adapter.SingleChooseAdapter;
import com.nic.view.model.CommonChooseBean;

import java.util.List;

/**
 * Created by Administrator on 2018/4/18.
 */

public class SinglePopWindow extends CommonPopupWindow implements View.OnClickListener, CommonCheckAdapter.OnClickItemSingleListener {
    private TextView tv_shadow;
    private MyGridView gv;
    private Button bt_cancel;
    private Button bt_confirm;


    private TextView tvDes;
    private SingleChooseAdapter adapter;
    private int position;
    private String currtId = "";
    private String currtName = "";
    private OnSingleListener mOnSingleListener;

    public SinglePopWindow(Context c) {
        super(c, R.layout.pop_mult, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        init();
    }

    public void setOnSingleListener(OnSingleListener mOnSingleListener) {
        this.mOnSingleListener = mOnSingleListener;
    }

    public void init() {
        contentView.findViewById(R.id.cb_all).setVisibility(View.GONE);
        tv_shadow = (TextView) contentView.findViewById(R.id.tv_shadow);
        tv_shadow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishPop();
            }
        });
        tvDes = (TextView) contentView.findViewById(R.id.tv_desc);
        gv = (MyGridView) contentView.findViewById(R.id.gv);
        bt_cancel = (Button) contentView.findViewById(R.id.cancel);
        bt_cancel.setOnClickListener(this);
        bt_confirm = (Button) contentView.findViewById(R.id.confirm);
        bt_confirm.setOnClickListener(this);
        // 设置弹出窗体需要软键盘，
        getPopupWindow().setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        // 再设置模式，和Activity的一样，覆盖，调整大小。
        getPopupWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        adapter = new SingleChooseAdapter();
        adapter.setClickItemSingleListener(this);
        gv.setAdapter(adapter);

    }

    public void setDesTitle(String text) {
        if (TextUtils.isEmpty(text)) {
            return;
        }
        tvDes.setText("" + text);
    }


    public void setList(List<CommonChooseBean> list) {
        adapter.setList(list);
    }

    public void showAsDropDown(View view, int position) {
        if (!getPopupWindow().isShowing() && !((Activity) context).isFinishing()) {
            this.position = position;
            checked();
            getPopupWindow().showAsDropDown(view);
        }
    }


    public void showAtLocation(View view, int position) {

        if (!getPopupWindow().isShowing() && !((Activity) context).isFinishing()) {
            this.position = position;
            checked();
            getPopupWindow().showAtLocation(view, Gravity.CENTER, 0, 0);
        }
    }

    public void show(int position) {
        if (!getPopupWindow().isShowing() && !((Activity) context).isFinishing()) {
            this.position = position;
            checked();
            getPopupWindow().showAtLocation(((Activity) context).getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        }
    }

    public boolean isShow() {
        return getPopupWindow().isShowing();
    }

    private void checked() {
        if (adapter.getList().isEmpty() || TextUtils.isEmpty(currtId)) {
            return;
        }
        adapter.checked(currtId);
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.cancel) {
            finishPop();
            getPopupWindow().dismiss();
        } else if (i == R.id.confirm) {
            getPopupWindow().dismiss();
            if (mOnSingleListener != null) {
                mOnSingleListener.onSingleItem(position, currtId, currtName);
            }
        }
    }


    public void finishPop() {
        currtId = "";
        currtName = "";
        adapter.setSelected(0);
    }

    @Override
    public void onItemClick(int position) {
        this.currtId = adapter.getList().get(position).getId();
        this.currtName = adapter.getList().get(position).getName();
    }

    public interface OnSingleListener {
        void onSingleItem(int position, String id, String name);

    }

    public void OnDismissListener(PopupWindow.OnDismissListener listener) {
        getPopupWindow().setOnDismissListener(listener);
    }
}
